package cn.udesk.web.controller;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.udesk.common.toolkit.Constant;
import cn.udesk.core.dto.BootTablePage;
import cn.udesk.core.dto.ResultDto;
import cn.udesk.core.model.KTrans;
import cn.udesk.core.model.KUser;
import cn.udesk.web.service.TransService;
import cn.udesk.web.utils.JsonUtils;

@RestController
@RequestMapping("/trans/")
public class TransController {

  @Autowired
  private TransService transService;


  @RequestMapping("getSimpleList.shtml")
  public String getSimpleList(HttpServletRequest request) {
    return JsonUtils.objectToJson(transService.getList());
  }

  @RequestMapping("getList.shtml")
  public String getList(Integer offset, Integer limit, HttpServletRequest request) {
    BootTablePage list = transService.getList(offset, limit);
    return JsonUtils.objectToJson(list);
  }

  @RequestMapping("uploadTrans.shtml")
  public String uploadJob(MultipartFile transFile, HttpServletRequest request) {
    KUser kUser = (KUser) request.getSession().getAttribute(Constant.SESSION_ID);
    String uuid = request.getParameter("uuid");
    try {
      String saveFilePath = transService.saveFile(kUser.getUId(), uuid, transFile);
      return ResultDto.success(saveFilePath);
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    }
  }

  @RequestMapping("insert")
  public String insert(KTrans kTrans, String customerQuarz, HttpServletRequest request) {
    KUser kUser = (KUser) request.getSession().getAttribute(Constant.SESSION_ID);
    Integer uId = kUser.getUId();
    if (transService.check(kTrans.getTransPath(), uId)) {
      try {
        transService.insert(kTrans, uId);
        return ResultDto.success();
      } catch (SQLException e) {
        e.printStackTrace();
        return ResultDto.fail("添加作业失败");
      }
    } else {
      return ResultDto.fail("该作业已经添加过了");
    }
  }

  @RequestMapping("getTrans.shtml")
  public String getTrans(Integer transId) {
    return ResultDto.success(transService.getTrans(transId));
  }

  @RequestMapping("update.shtml")
  public String update(KTrans kTrans, String customerQuarz, HttpServletRequest request) {
    KUser kUser = (KUser) request.getSession().getAttribute(Constant.SESSION_ID);
    try {
      transService.update(kTrans, kUser.getUId());
      return ResultDto.success();
    } catch (Exception e) {
      return ResultDto.success(e.toString());
    }
  }

  @RequestMapping("delete.shtml")
  public String delete(Integer transId, HttpServletRequest request) {
    try {
      transService.delete(transId);
      return ResultDto.success();
    } catch (Exception e) {
      return ResultDto.success(e.toString());
    }
  }

  @RequestMapping("start.shtml")
  public String start(Integer transId) {
    transService.start(transId);
    return ResultDto.success();
  }

  @RequestMapping("stop.shtml")
  public String stop(Integer transId) {
    transService.stop(transId);
    return ResultDto.success();
  }
}
